package com.yuji.aacar.base;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.yuji.aacar.R;
import com.yuji.aacar.core.cores.event.CoreEventBusMessage;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.core.mvvm.appbase.AppRoute;
import com.yuji.aacar.core.mvvm.appbase.CoreBaseFragmentActivity;
import com.yuji.aacar.core.mvvm.appbase.CoreBaseViewModel;
import com.yuji.aacar.tools.StatusBarUtils;

/**
 * <li>Package:com.yuji.aacar.base</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/2/28</li>
 * <li>Description:   </li>
 */

public class BaseFragmentActivity extends CoreBaseFragmentActivity{

    private Dialog loadDialog;//加载框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBarColor(R.color.titleBarColor);
        PushAgent.getInstance(this).onAppStart();
    }

    @Override
    public void setViewModel(CoreBaseViewModel coreBaseViewModel) {

    }

    @Override
    public CoreBaseViewModel getViewModel() {
        return null;
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
    }

    @Override
    public void requestFailed(CoreServiceResponse response) {

    }

    @Override
    public void alreadyBindBaseViewModel() {

    }

    @Override
    public void showToast(String text) {

    }

    @Override
    public void showToast(String text, int duration) {

    }

    @Override
    public void cancelToast() {

    }

    public void setTitleBarColor (int colorId) {
        StatusBarUtils.with(this).setColor(getResources().getColor(colorId)).init();
    }

    @Override
    public void handleMessage(CoreEventBusMessage message) {
        super.handleMessage(message);
    }

    /***************
     * Activity跳转
     * @param activity
     */
    public void presentController(Class<? extends BaseFragmentActivity> activity) {
        AppRoute.getInstance().nextController(this, activity, AppRoute.WITHOUT_RESULTCODE);
    }

    /***************
     * Activity跳转
     * @param activity
     * @param intent
     */
    public void presentController(Class<? extends BaseFragmentActivity> activity, Intent intent) {
        AppRoute.getInstance().nextControllerWithIntent(this, activity, AppRoute.WITHOUT_RESULTCODE, intent);
    }


    /********
     * Activity跳转
     * @param activity
     * @param requestCode
     */
    public void presentController(Class<? extends BaseFragmentActivity> activity, int requestCode) {
        AppRoute.getInstance().nextController(this, activity, requestCode);
    }

    /********
     * Activity跳转
     * @param activity
     * @param intent
     * @param requestCode
     */
    public void presentController(Class<? extends BaseFragmentActivity> activity, Intent intent, int requestCode) {
        AppRoute.getInstance().nextControllerWithIntent(this, activity, requestCode, intent);
    }

    /***
     * 显示Loading框
     ***/
    public void showProgress() {
        if (loadDialog == null) {
//            loadDialog = new LoadingDialog(this);
            loadDialog.setCancelable(false);
            loadDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                        // cancel掉请求的服务
                        //                        cancelTask(coreBaseViewModel.getTaskToken());
                        if (loadDialog != null && loadDialog.isShowing()) {
                            loadDialog.dismiss();
                            loadDialog = null;
                            return true;
                        }

                        // 退出应用时，释放单例类
                    }
                    return false;
                }
            });
        }
        if (!loadDialog.isShowing()) {
            loadDialog.show();
        }
    }

    /***
     * 关闭Loading框
     ***/
    public void dismissProgress() {
        if (loadDialog != null && loadDialog.isShowing()) {
            loadDialog.dismiss();
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
